package com.android.jay.lib_commin_ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * Created by wzp on 2020/2/24 0024.
 * <p>
 * 描述  ：仿抖音主页下拉放大
 */
public class FlexibelLayout extends LinearLayout {
    /**
     * 是否是下拉
     **/

    private boolean mIsBeingDragged;

    /**
     * 初始坐标
     **/

    private float mInitialY, mInitialX;

    //头部
    private View mHeader;

    private int mHeaderHeight = 0;
    private int mHeaderWidth = 0;
    private boolean mIsReadyHead;

    /**
     * 下拉监听
     */
    private OnPullListener mOnPullListener;


    /**
     * 最大下拉高度
     */
    private int mMaxPullHeight = 200;

    /**
     * 是否允许下拉放大
     */
    private boolean isEnable = true;

    public FlexibelLayout(Context context) {
        this(context, null);
    }

    public FlexibelLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlexibelLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (isEnable) {
//            Log.e("onInterceptTouchEvent", ev.getAction() + "");
//            switch (ev.getAction()) {
//                case MotionEvent.ACTION_MOVE:
//
//                case MotionEvent.ACTION_DOWN:
//                    mIsBeingDragged = false;
//                    mInitialX = ev.getX();
//                    mInitialY = ev.getY();
//
//                    break;
//
//            }
//        }
//        return true;
//    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isEnable) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (mIsBeingDragged) {
                        resetHeader();
                        if (mOnPullListener != null) {
                            mOnPullListener.onRelease();
                        }
                    }
                    break;
                case MotionEvent.ACTION_DOWN:
                    if (!mIsBeingDragged) {
                        mIsBeingDragged = false;
                        mInitialX = event.getX();
                        mInitialY = event.getY();
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    //如果是下拉 则转给TouchEvent
                    float diffX = event.getX() - mInitialX;
                    float diffY = event.getY() - mInitialY;
                    if (diffY > 0 && diffY / Math.abs(diffX) > 2) {
                        mIsBeingDragged = true;
                        //得到下拉的距离
                        float Y = event.getY() - mInitialY;
                        changeHeader((int) Y);
                        if (mOnPullListener != null) {
                            mOnPullListener.onPull((int) Y);
                        }
                        return true;
                    }
                    break;
            }
        }
        return true;
    }


    private void resetHeader() {
        mHeader.getLayoutParams().height = mHeaderHeight;
        mHeader.getLayoutParams().width = mHeaderWidth;
        mHeader.setTranslationX(0);
        mHeader.requestLayout();
    }

    private void changeHeader(int diffY) {

        int pulloffset = (int) Math.pow(diffY, 0.8);
        int newHeight = pulloffset + mHeaderHeight;
        int newWidth = (int) (((float) (newHeight / mHeaderHeight)) * mHeaderWidth);
        mHeader.getLayoutParams().width = newWidth;
        mHeader.getLayoutParams().height = newHeight;
        int margin = (newWidth - mHeaderWidth) / 2;
        mHeader.setTranslationX(-margin);
        mHeader.requestLayout();


    }

    public FlexibelLayout setHeader(View view) {
        mHeader = view;
        view.post(new Runnable() {
            @Override
            public void run() {
                mHeaderHeight =mHeader.getHeight();
                mHeaderWidth = mHeader.getWidth();
                mIsReadyHead = true;
            }
        });

        return this;
    }


    //设置最大下拉高度
    public void setPullMax(int height) {
        mMaxPullHeight = height;
    }

    //设置是否能下拉
    public void setEnable(boolean isEnable) {
        this.isEnable = isEnable;
    }

    public void setPulllistener(OnPullListener listener) {
        mOnPullListener = listener;
    }

    public interface OnPullListener {
        void onPull(int offset);

        void onRelease();
    }
}
